package gates;

import java.awt.Graphics;

import master.*;



/**
 * A phase shift gate built using the sparse matrix class. Should work applying the phase shift to any bit
 * and using a register of any size.
 * 
 * if the bit in question is a one will change to 1 + i, giving a phase shift of Pi/4, if a 0, 
 * it will remain a 0
 * 
 * @author Matthew
 *
 */



public class PhaseShift implements QuantumGate{// implements QuantumGate{

	int dim;
	int bit;
	int limit;
	int count;//gives row and column number of matrix
	
	public PhaseShift(int N, int bit){
		count = 0;
		dim = (int)Math.pow(2, N);
		limit = (int) ( dim/(Math.pow(2, bit)));
	
		
	}
 		
	
	
	
	public boolean checkDim(QuantumRegister register){
		//checks dimension of matrix compared to dimension of register
		if (register.getDimension() == dim){
			return true;
		}else{
			return false;
		}
	}
	
	public QuantumRegister actOnReg(QuantumRegister register, int bit){
		count = 0;
		
		for (int j = 0; j < limit; j++){
			//TODO Test
				for (int i = 0; i < (int)Math.pow(2,bit); i++){
					if ( j%2 != 0 && (register.getCoefficient(count) != new ComplexNumber())){
						
						//currently does Pi/4 phase change, generalise more
						//make it so when applied more than once doesn't still give pi/4
						ComplexNumber temp = new ComplexNumber(register.getCoefficient(count).getReal(), register.getCoefficient(count).getImaginary() + register.getCoefficient(count).getReal());
						System.out.println("is this nan?" + temp);
						System.out.println("" + temp.magnitude());
						ComplexNumber temp1 = temp.multByNo( (float) 1 / temp.magnitudeFloat() );
						System.out.println("hello" + temp1);
						register.setCoefficient(temp1, count);
					}
					count ++;
					
				}
			}		

			
			
				

		
		
		return register;
	}




	@Override

	public QuantumRegister actOnReg(QuantumRegister register, int bit1, int bit2) {
		// TODO Auto-generated method stub
		return null;
	}




	@Override
	public QuantumRegister actOnReg(QuantumRegister register, int bit1,
			int bit2, int bit3) {
		// TODO Auto-generated method stub
		return null;
	}




	@Override
	public QuantumGate multiply(QuantumGate gate) {
		// TODO Auto-generated method stub
		return null;
	}




	@Override
	public void draw(Graphics g, int x, int y, float scale) {
		// TODO Auto-generated method stub
		
	}
}
